<?php 
global $base_url;
$service = $base_url;
$redirect = url('welcome',array('absolute' => TRUE));
$server_cas_server = (string)variable_get('cas_server', 'sso-cas.univ-rennes1.fr');
$server_port       = (int)variable_get('cas_port', '443');
$server_uri        = (string)variable_get('cas_uri', '');
$protcal = ($server_port == '443') ? 'https://' : 'http://';
$cas_url = $protcal.trim($server_cas_server,'/').'/'.trim($server_uri,'/').'/login';
$action_url = $cas_url.'?service='.$base_url;

?>

<form accept-charset="utf-8" action="<?php print $action_url;?>" method="post" onsubmit="return loginValidate();" target="ssoLoginFrame">  
        <ul>  
            <span class="red" style="height:12px;" id="J_ErrorMsg"></span>       
            <li>  
                <label><em><?php print t('Username');?>:</em></label>
                <input name="username" id="J_Username" type="text" autocomplete="off" class="line" style="width: 180px" />  
            </li>  
            <li>  
                <label><em><?php print t('Password');?></em></label>  
                <input name="password" type="password"  id="J_Password" class="line" style="width: 180px" />  
            </li>
                <input type="hidden" name="isajax" value="true" />  
                <input type="hidden" name="isframe" value="true" />  
                <input type="hidden" name="lt" value="" id="lt">  
				<input type="hidden" name="execution" value="" id="execution">    
                <input type="hidden" name="_eventId" value="submit" />  
                <input name="" type="submit" value="<?php print t('Login');?>" class="loginbanner" />  
        </ul>  
    </form>  
<script type="text/javascript">
var $ = jQuery;
document.domain = 'crownrelo.com';
$(document).ready(function(){   
            flushLoginTicket();  
        });  
var loginValidate = function(){  
    var msg;  
    if ($.trim($('#J_Username').val()).length == 0 ){  
        msg = "username can't be empty";  
    } else if ($.trim($('#J_Password').val()).length == 0 ){  
        msg = "password can't be empty";  
    }  
    if (msg && msg.length > 0) {  
        $('#J_ErrorMsg').fadeOut().text(msg).fadeIn();  
        return false;  
        // Can't request the login ticket.  
    } else if ($('#lt').val().length == 0){  
        $('#J_ErrorMsg').text('CAS Server is busying now, please try it later...');  
        return false;  
    } else {   
        $('body').append($('<iframe/>').attr({  
            style: "display:none;width:0;height:0",   
            id: "ssoLoginFrame",  
            name: "ssoLoginFrame",  
            src: "javascript:false;"  
        }));  
        return true;  
    }  
};  

var feedBackUrlCallBack = function (result) {
    customLoginCallBack(result);  
    deleteIFrame('#ssoLoginFrame');
};  
  

var customLoginCallBack = function(result){  

    if (result.login == 'fails'){  
        $('#J_ErrorMsg').fadeOut().text(result.msg).fadeIn();  
    
        flushLoginTicket();  
    }
    else{
       //
	   window.location= "<?php print $redirect; ?>";
   }	
};  
  
var deleteIFrame = function (iframeName) {  
    var iframe = $(iframeName);   
    if (iframe) { 
        iframe.remove()  
    }  
};  
  
var flushLoginTicket = function(){
    var _services = 'login-at=' + encodeURIComponent('<?php print $base_url;?>');  
    $.getScript('<?php print $cas_url; ?>?'+_services+'&get-lt=true&n='   
            + new Date().getTime(), 
    function(){
        $('#lt').val(_lt);  
		$('#execution').val(_execution);  
    });  
} 
</script>